package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.vo_封装参数为对象传给前端.CourseInfoVo;
import com.atguigu.eduservice.entity.vo_封装参数为对象传给前端.CoursePublishVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.conf.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-11-20
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    /**
     * 添加课程的service服务，会向两个表插入信息，课程表，简介表，其中简介表需要与课程表的id对应
     */

    //课程描述注入
    @Autowired
    private EduCourseService eduCourseService;


    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;


    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;



    //添加课程基本信息的方法
    @Override
    //1 向课程表添加课程基本信息
    //CourseInfoVo对象转换eduCourse对象
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //CourseInfoVo对象转换eduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0) {
            throw new GuliException(20001, "添加课程信息失败");
        }
        String eduCouseId = eduCourse.getId();

        //2 向课程简介表添加课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(eduCouseId);
        eduCourseDescriptionService.save(eduCourseDescription);
        return eduCouseId;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //根据courseid获取信息
//        课程信息两部分，一部分是课程表里面的信息，
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        //一部分是课程描述表里面的信息，获取之后返回CourseInfoVo
        EduCourseDescription eduCourseDescriptionServiceById = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(eduCourseDescriptionServiceById.getDescription());

        return courseInfoVo;

    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //更新课程信息，课程信息两部分，一部分是课程表里面的信息，
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int numOfUpdateById = baseMapper.updateById(eduCourse);
        if (numOfUpdateById == 0) {
            throw new GuliException(20001, "修改课程信息失败");
        }

        //一部分是课程描述表里面的信息，获取之后返回CourseInfoVo
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
//尚硅谷写法
//        description.setDescription(courseInfoVo.getDescription());
//        description.setId(courseInfoVo.getId());
        BeanUtils.copyProperties(courseInfoVo, eduCourseDescription);
        eduCourseDescriptionService.updateById(eduCourseDescription);

    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    //需要根据课程的id，删除课程的小节，章节，描述，课程本身，多表的内容删除，涉及对多个表的crud
    @Override
    public void removeCourse(String courseId) {
        //1 根据课程id删除小节
        eduVideoService.removeByCourseId(courseId);
        //2 根据课程id删除章节
        eduChapterService.removeByCourseId(courseId);
        //3 根据课程id删除描述
        eduCourseDescriptionService.removeById(courseId);
        //4 根据课程id删除课程本身
        int result = baseMapper.deleteById(courseId);
        if(result ==0){
            throw new GuliException(20001, "删除失败");
        }

    }

}